import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.print.attribute.standard.Finishings;

public class UDPListener {
	private static DatagramSocket udpSocket;
	private static byte[] data = new byte[256];
	private static DatagramPacket udpPacket = new DatagramPacket(data,
			data.length);

	public static void main(String[] args) throws UnknownHostException {
		try {
			udpSocket = new DatagramSocket(43708);
		} catch (SocketException e) {
		}
		Socket socket = null;
		while (true) {
			try {
				udpSocket.receive(udpPacket);
			} catch (Exception e) {
			}

			if ( null != udpPacket.getAddress()) {
				String codeString = new String(data, 0, udpPacket.getLength());
				codeString += udpPacket.getAddress();
				System.out.println(codeString);
				try {
					String ip = udpPacket.getAddress().toString().substring(1);
					System.out.println(ip);
					socket = new Socket(ip, 8080);

				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						socket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}
}